package h0723;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 手机防沉迷系统
 * @date 2024/7/24 0:18
 */
class App {
    String name;
    int priority;
    String startTime;
    String endTime;

    public App(String name, int priority, String startTime, String endTime) {
        this.name = name;
        this.priority = priority;
        this.startTime = startTime;
        this.endTime = endTime;
    }
}

public class AntiAddictionSystem {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取App数量
        int n = Integer.parseInt(scanner.nextLine());

        //存放注册app的list
        List<App> apps = new ArrayList<>();

        // 读取App注册数据
        for (int i = 0; i < n; i++) {
            String[] input = scanner.nextLine().split(" ");
            String name = input[0];
            int priority = Integer.parseInt(input[1]);
            String startTime = input[2];
            String endTime = input[3];
            if (startTime.compareTo(endTime) < 0) {
                //添加步骤
                apps.add(new App(name, priority, startTime, endTime));
            }
        }

        // 读取时间点
        String queryTime = scanner.nextLine();

        // 查找在给定时间点的App
        String result = "NA";
        int highestPriority = -1;

        //TODO
        for (App app : apps) {
            if (app.startTime.compareTo(queryTime) <= 0 && queryTime.compareTo(app.endTime) < 0) {
                if (app.priority > highestPriority) {
                    highestPriority = app.priority;
                    result = app.name;
                }
            }
        }

        // 输出结果
        System.out.println(result);
    }
}
